from django.shortcuts import render

# Create your views here.




from fdfs_client.client import Fdfs_client

# client = Fdfs_client('utils/fdfs/client.conf')
#
# client.upload_by_filename('/home/python/Pictures/1.png')
from rest_framework.generics import ListAPIView

from goods.models import SKU
from goods.serializers import SKUAPIViewSerializer
'''
１　全都查询出来
２　分页
３　价格排序

'''
from rest_framework.filters import OrderingFilter


class HotSKUAPIView(ListAPIView):
    # 热销视图不用分页
    pagination_class = None
    serializer_class = SKUAPIViewSerializer

    def get_queryset(self):
        category_id = self.kwargs.get('category_id')

        return SKU.objects.filter(category = category_id,is_launched=True).order_by('-sales')[:2]


class SKUAPIView(ListAPIView):
    serializer_class = SKUAPIViewSerializer

    # 添加排序
    filter_backends = [OrderingFilter]
    # 根据列表中的字段进行排序
    ordering_fields = ['sales', 'price', 'create_time']

    def get_queryset(self):
        category_id = self.kwargs.get('category_id')
        # page 第几页　　page_size 是多少条数　

        return SKU.objects.filter(category = category_id,is_launched=True)


from .serializers import SKUAPIViewSerializer
from drf_haystack.viewsets import HaystackViewSet


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUAPIViewSerializer